#include "task/facenet.h"
 

float eu_distance(float* input)
{
	float sum = 0;
	for (int i = 0; i < 128; ++i)
	{
		sum = sum + input[i] * input[i];
	}
	sum = sqrt(sum);
	return sum;
}

float compare_eu_distance(float* input1, float* input2)
{
	float sum = 0;
	for (int i = 0; i < 128; ++i)
	{
		sum = sum + (input1[i] - input2[i]) * (input1[i] - input2[i]);
	}
	sum = sqrt(sum);
	return sum;
}

float cos_similarity(float* input1, float* input2)
{
	float sum = 0;
	for (int i = 0; i < 128; ++i)
	{
		sum = sum + input1[i] * input2[i];
	}
	float tmp1 = eu_distance(input1);
	float tmp2 = eu_distance(input2);
	return sum / (tmp1 * tmp2);
}


int main(int argc, char **argv)
{
    facenetCustom facenet;
    // model file path
    const char *model_file = argv[1];
    // input img path
    const char *img_file = argv[2];
    // load image
    cv::Mat img = cv::imread(img_file);
    cv::Mat origin_img = img;
    printf("The img size is %d , %d\n", img.cols, img.rows);
    // load model file
    facenet.LoadModel(model_file);
    float res_vector[128];
    facenet.Run(img, res_vector);

    // cv::Mat img2 = cv::imread("/mnt/sdcard/yp/facenet/data/img/lin_2.jpg");
    // float res_vector2[128];
    // facenet.Run(img2, res_vector2);

    // facenet.SaveEmbeddings(res_vector2, "../dataset/lin_2.data");
    // float tmp2, tmp3;

    std::ifstream infile("../dataset/lin_2.data");
    std::string tmp;
    float res_vector2[128];
    float tmp2, tmp3;
    
    int i = 0;
    while (getline(infile, tmp))
    {
        res_vector2[i] = atof(tmp.c_str());
        i++;
    }
    infile.close();

    tmp2 = compare_eu_distance(res_vector, res_vector2);
    tmp3 = cos_similarity(res_vector, res_vector2);
    printf("eu_distance:%f\n", tmp2);
    printf("cos_similarity:%f\n", tmp3);

    // 使用完毕后记得释放内存
    // delete[] res_vector;
    return 0;
}
